package com.ms.p2p.controller.admin;


import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.common.bean.Message;
import com.ms.common.exception.NoticeException;
import com.ms.common.utils.AppContext;
import com.ms.common.utils.MsDateUtils;
import com.ms.common.utils.excel.ExcelData;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.controller.BaseController;
import com.ms.p2p.entity.account.AccountRecharge;
import com.ms.p2p.entity.member.Member;
import com.ms.p2p.enums.AccountFLow;
import com.ms.p2p.enums.AccountType;
import com.ms.p2p.plugin.validation.annotation.RequiredStringValidator;
import com.ms.p2p.plugin.validation.annotation.Validations;
import com.ms.p2p.provider.MemberAccountProvider;
import com.ms.p2p.provider.MemberAccountRechargeProvider;
import com.ms.p2p.provider.MemberProvider;
import com.ms.p2p.utils.JsonDataGridHelper;
import com.ms.p2p.utils.TradeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Controller("AdminAccountController")
@RequestMapping({"/admin/account"})
public class AdminAccountController extends AdminBaseController {

    @Reference
    private MemberAccountProvider memberAccountProvider;

    @Reference
    private MemberAccountRechargeProvider memberAccountRechargeProvider;

    @Reference
    private MemberProvider memberProvider;

    @RequestMapping({"/usertuiguang"})
    public String usertuiguang(HttpServletRequest request) {
        PageBean page = new PageBean();

        List<Map<String, Object>> accounts = memberAccountProvider.FindTG();

        request.setAttribute("tg", accounts);

        return "admin/account/record/spread/list";
    }

    @RequestMapping({"/everyday"})
    public String everyday(HttpServletRequest request) {
        return "admin/account/record/everyday/list";

    }
    @RequestMapping({"/everydayData"})
    @ResponseBody
    public PageBean everydayData(HttpServletRequest request, PageBean page) {
        page.setPageSize(10000);
        page.setList(memberAccountProvider.geteveryday(page));
        return JsonDataGridHelper.createJSONData(page);

    }

    @RequestMapping({"/tenderListData"})
    @ResponseBody
    public PageBean tenderListData(   @RequestParam(value = "time_limit", required = false) String time_limit
            , @RequestParam(value = "begin_date", required = false) String begin_date
            , @RequestParam(value = "end_date", required = false) String end_date,PageBean page) {



        page.setList( memberAccountProvider
                .findtenderListPager(time_limit, begin_date, end_date, page));
        page.setTotalCount(memberProvider.countAllUser());
        return JsonDataGridHelper.createJSONData(page);
    }


    @RequestMapping({"/tenderList"})
    public String tenderList(HttpServletRequest request



    ) {

        return "admin/account/record/tendercondition/list";

    }

    @RequestMapping({"/exporttenderList"})
    public String tenderList(HttpServletRequest request, Model model
            , @RequestParam(value = "time_limit", required = false) String time_limit
            , @RequestParam(value = "begin_date", required = false) String begin_date
            , @RequestParam(value = "end_date", required = false) String end_date

    ) {
        PageBean page = new PageBean();
        List<Map<String, Object>> accounts = memberAccountProvider
                .findtenderListPager(time_limit, begin_date, end_date, page);
        for (Map<String, Object> map : accounts) {
            Object total = map.get("user_name");
            if (total != null) {
                map.put("user_name", "" + total.toString());
            }
            Object money = map.get("money");
            if (money != null) {
                map.put("money", money.toString());
            }
            Object balance = map.get("name");
            if (balance != null) {
                map.put("name", balance.toString());
            }

        }
        ExcelData excelData = new ExcelData();
        excelData.setHeads(new String[]{"用户名", "金额", "客服"});
        excelData.setFieldNames(new String[]{"user_name", "money", "name"});
        excelData.setDataList(accounts);
        excelData.setModelClass(Map.class);
        model.addAttribute("excelData", excelData);
        model.addAttribute("fileName", "月投标待收统计");

        return "excel";

    }

    @RequestMapping({"/getkhtj"})
    public String getkhtj(String realname,
                          String begin_date, String end_date,
                          String userId, String userName,
                          String is_day, String currentPage, Model model) {

        return "admin/account/record/tendercollection/list";
    }
    @RequestMapping({"/getkhtjData"})
    @ResponseBody
    public PageBean getkhtjData(String realname,
                                String begin_date, String end_date,
                                String userId, String userName,
                                String is_day, PageBean page, Model model) {
        page.setList(memberAccountProvider.findCollectioListPager(userId, userName, realname, begin_date, end_date, is_day, page));
        page.setTotalCount(memberProvider.countAllUser());
        return JsonDataGridHelper.createJSONData(page);
    }

    @RequestMapping({"/exportgetkhtj"})
    public String exportgetkhtj(String realname,
                                String begin_date, String end_date,
                                String userId, String userName,
                                String is_day, Model model) {

        PageBean page = new PageBean();
        page.setPageSize(null);
        List<Map<String, Object>> accounts = memberAccountProvider.findCollectioListPager(userId, userName, realname, begin_date, end_date, is_day, page);
        for (Map<String, Object> map : accounts) {
            Object total = map.get("user_name");
            if (total != null) {
                map.put("user_name", "" + total.toString());
            }
            Object real_name = map.get("real_name");
            if (real_name != null) {
                map.put("real_name", real_name.toString());
            }
            Object money = map.get("account");
            if (money != null) {
                map.put("account", money.toString());
            }
            Object balance = map.get("wait_account");
            if (balance != null) {
                map.put("wait_account", balance.toString());
            }
        }
        ExcelData excelData = new ExcelData();
        excelData.setHeads(new String[]{"用户名", "真实姓名", "投资金额", "待收金额", "客服名称"});
        excelData.setFieldNames(new String[]{"user_name", "real_name", "account", "wait_account", "name"});
        excelData.setDataList(accounts);
        excelData.setModelClass(Map.class);
        model.addAttribute("excelData", excelData);
        model.addAttribute("fileName", "客户投标待收");
        return "excel";
    }

    @RequestMapping({"/exportusertuiguang"})
    public String exportusertuiguang(HttpServletRequest request, Model model) {
        PageBean page = new PageBean();
        List<Map<String, Object>> accounts = memberAccountProvider.FindTG();
        for (Map<String, Object> map : accounts) {
            Object total = map.get("id");
            if (total != null) {
                map.put("id", total.toString());
            }
            Object money = map.get("user_name");
            if (money != null) {
                map.put("user_name", "" + money.toString());
            }
            Object balance = map.get("real_name");
            if (balance != null) {
                map.put("real_name", "" + balance);
            }

            Object sumperaccount = map.get("sumperaccount");
            if (sumperaccount != null) {
                map.put("sumperaccount", "" + sumperaccount);
            }
            Object tccount = map.get("tccount");
            if (tccount != null) {
                map.put("tccount", "" + tccount.toString());
            }

        }

        ExcelData excelData = new ExcelData();
        excelData.setHeads(new String[]{"用户ID", "用户名", "实名", "金额", "人数"});
        excelData.setFieldNames(new String[]{"id", "user_name", "real_name",
                "sumperaccount", "tccount"});
        excelData.setDataList(accounts);
        excelData.setModelClass(Map.class);
        model.addAttribute("excelData", excelData);
        model.addAttribute("fileName", "资金使用记录");
        return "excel";

    }

    @RequestMapping({"/addRecharge"})
    public String addRecharge(HttpServletRequest request) {
        return "admin/account/record/addrecharge/input";

    }

    @RequestMapping(value = {"/Recharge"})
    @ResponseBody
    @Validations(requiredStrings = {
            @RequiredStringValidator(fieldName = "username", message = "用户名不能为空"),
            @RequiredStringValidator(fieldName = "remark", message = "备注不能为空")
    })
    public Message Recharge(HttpServletRequest request
            , @RequestParam(value = "username", required = false) String username
            , @RequestParam(value = "remark", required = false) String remark
    ) throws IllegalAccessException, IntrospectionException, InvocationTargetException {
        Member user = memberProvider.findByUserName(username);
        if (user == null) {
            throw new NoticeException("没有这个用户");
        }
        if (request.getParameter("money").contains("-")) {
            String money = request.getParameter("money").substring(1,
                    request.getParameter("money").length());
            memberAccountProvider.OperAccount(AppContext.getRemoteIp(),new BigDecimal(money),user.getId(),remark, AccountType.bank_manage.getValue(), AccountFLow.FLOW_OUT.getValue());

        } else {

            AccountRecharge recharge = new AccountRecharge();
            recharge.setUser_id(user.getId());
            recharge.setAddtime(MsDateUtils.getTime());
            recharge.setAddip(AppContext.getRemoteIp());
            recharge.setStatus(0);
            recharge.setMoney(new BigDecimal(request.getParameter("money")));

            recharge.setFee(new BigDecimal(0));
            recharge.setAmount(recharge.getMoney().subtract(
                    new BigDecimal(0)));
            recharge.setType(0);
            recharge.setPayment(4);
            recharge.setTrade_no(TradeUtil.getTrano() + "");
            recharge.setRemark(remark);
            memberAccountRechargeProvider.insert(recharge);

        }
        return Message.success("充值成功");
    }
    @RequestMapping(value = {"/queryData"}, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public PageBean queryData(  @RequestParam(value = "userName", required = false) String userName
            , @RequestParam(value = "userId", required = false) String userId
            , @RequestParam(value = "currentPage", required = false) String currentPage
            , @RequestParam(value = "begin_date", required = false) String begin_date
            , @RequestParam(value = "end_date", required = false) String end_date
                             , PageBean page
    ) {

       page.setList( memberAccountProvider.findPager(userId,
               userName, begin_date, end_date, page));
        page.setTotalCount(memberAccountProvider.findCount(userId, userName,
                begin_date, end_date));
        return JsonDataGridHelper.createJSONData(page);
    }

    @RequestMapping({"/list"})
    public String list(HttpServletRequest request

    ) {


        return "admin/account/record/accountinfo/list";
    }


    @RequestMapping({"/exportList"})
    public String exportList(@RequestParam(value = "userName", required = false) String userName
            , @RequestParam(value = "userId", required = false) String userId
            , @RequestParam(value = "begin_date", required = false) String begin_date
            , @RequestParam(value = "end_date", required = false) String end_date, Model model) {
        PageBean page = new PageBean();
        List<Map<String, Object>> accounts = memberAccountProvider.findAll(userId,
                userName, begin_date, end_date);
        for (Map<String, Object> map : accounts) {
            String u_addtime = (String) map.get("u_addtime");
            if (u_addtime != null) {
                map.put("u_addtime", MsDateUtils.TimeStamp2DateTime(u_addtime));
            }
            Object total = map.get("total");
            if (total != null) {
                map.put("total", total.toString());
                BigDecimal t = BigDecimal.ZERO;
                t = t.add(BigDecimal.valueOf(Double.valueOf(String.valueOf(total))));
                Object collection = map.get("collection");
                if (collection != null) {
                    t = t.add(BigDecimal.valueOf(Double.valueOf(String.valueOf(collection.toString()))));
                }
                map.put("total", t.doubleValue() + "");
            }
        }
        ExcelData excelData = new ExcelData();
        excelData.setHeads(new String[]{"用户ID", "用户名", "账户总额", "可用余额", "冻结金额", "活期宝总额", "待收金额", "注册时间"});
        excelData.setFieldNames(new String[]{"user_id", "u_userName", "total", "balance", "freezing_amont", "yuebao_total", "collection", "u_addtime"});
        excelData.setDataList(accounts);
        excelData.setModelClass(Map.class);
        model.addAttribute("excelData", excelData);
        model.addAttribute("fileName", "用户账户信息");
        return "excel";

    }


    @Override
    public boolean getBinderFlag() {
        return true;
    }
}
